package com.team.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.team.entity.Goal;
import com.team.entity.Project;
import com.team.util.DbHelp;
import com.team.util.ResultSetPackge;

public class GoalDao {

	DbHelp<Goal> db = new DbHelp<Goal>();
	
	public int saveGoal(Goal goal){
		String sql = "insert into t_goal(goalname,content,createtime,t_user_id,t_project_id)values(?,?,?,?,?)";
		return db.executeUpdateSql(sql,goal.getGoalName(),goal.getContent(),goal.getCreateTime(),goal.getUserId(),goal.getProjId() );
	}
	
	
	public  List<Goal> selectGoalsByProjId(Project project){
		String sql = "select id,goalname,content,createtime,t_user_id,t_project_id from t_goal where t_project_id =?";
		return db.exeSelectForList(sql, new PackgeGoal(), project.getId());
	}
	
	public Goal selectGoalById(int id ){
		String sql = "select id,goalname,content,createtime,t_user_id,t_project_id from  t_goal where id = ?";
		return db.exeSelectForObject(sql, new PackgeGoal(), id);
	}
	private class PackgeGoal implements ResultSetPackge<Goal>{

		public Goal packgeResultSet(ResultSet rs) throws SQLException {
			Goal goal = new Goal();
			goal.setGoalName(rs.getString("goalname"));
			goal.setContent(rs.getString("content"));
			goal.setCreateTime(rs.getString("createtime"));
			goal.setUserId(rs.getInt("t_user_id"));
			goal.setProjId(rs.getInt("t_project_id"));
			goal.setId(rs.getInt("id"));
			return goal;
		}
		
		
	}
	public int updateGoal(String name, String content, int id) {
		String sql = "update t_goal set goalname=?,content=? where id = ?";
		return db.executeUpdateSql(sql,name,content, id);
	}
	
}  
